package com.xgc.oauth.custom;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class OAuth2Reaml extends AuthorizingRealm {


    /**
     *
     *  默认使用的token为：{@link org.apache.shiro.authc.UsernamePasswordToken}
     *  ，所以需要重写support方法，不然会报错token不匹配
     *  <p>或者在realm中设置authenticationTokenClass属性为你想要使用的Token</p>
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof BearerToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        BearerToken bearerToken = (BearerToken) authenticationToken;
        String token = bearerToken.getToken();
        String host = bearerToken.getHost();
        System.out.println("token:"+token);
        System.out.println("host:"+host);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(host, token, getName());
        return info;
    }
}
